package io.lb.deng.webflux.web.controller;

import java.time.LocalDateTime;

import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import io.lb.deng.starter.SnowFlake;
import io.lb.deng.webflux.api.model.UserVo;
import io.lb.deng.webflux.api.serviceapi.IUserService;
import io.lb.deng.webflux.web.component.JwtTokenProvider;
import io.lb.deng.webflux.web.model.ResponseData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import reactor.core.publisher.Mono;

/**
 * @author DengLbin
 * @date Thu Sep 30 12:00:16 CST 2021
 */
@RestController
@RequestMapping("/user")
@Api(tags = { "1、用户接口" })
public class UserController {

    @DubboReference
    private IUserService userService;
    @Autowired
    private SnowFlake snowFlake;

    @Autowired
    private JwtTokenProvider tokenProvider;

    @ApiOperation(value = "hello测试接口")
    @GetMapping(value = "hello")
    public ResponseData<UserVo> hello() {
        return ResponseData.success(new UserVo(snowFlake.nextId(), "李四", LocalDateTime.now()));
    }

    @ApiOperation(value = "monoJust")
    @GetMapping(value = "monoJust")
    public Mono<ResponseData<UserVo>> monoJust() {
        return Mono.just(ResponseData.success(new UserVo(snowFlake.nextId(), "李四", LocalDateTime.now())));
    }

    @ApiOperation(value = "登录")
    @PostMapping(value = "login")
    public Mono<ResponseData<String>> login(@RequestBody UserVo vo) {
        

        return Mono.just(ResponseData.success(tokenProvider.createToken(vo.getUsername(), 1L, "ROLE_USER,ROLE_ADMIN, user_edit")));
    }

    @ApiOperation(value = "getOneUser")
    @GetMapping(value = "getOneUser")
  
    public Mono<ResponseData<UserVo>> getOneUser() {

        
        return Mono.just(ResponseData.success(userService.getOne()));
    }

}
